﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using saigonbook.Dal;

namespace saigonbook.Biz
{
    public class BIZ_loaisach
    {
        public int maloai;
        public string tenloai;
        public int Maloai
        {
            get { return maloai; }
            set { maloai = value; }
        }
        public string Tenloai
        {
            get { return tenloai; }
            set { tenloai = value; }
        }
        public BIZ_loaisach()
        { }
        public BIZ_loaisach(int maloai, string tenloai)
        {
            this.maloai = maloai;
            this.tenloai = tenloai;
        }
        public static List<BIZ_loaisach> Select_loaisach()
        {
            return DAL_sach.Select_loaisach();
        }

        public static List<BIZ_loaisach> Search_LoaiSach(string ten)
        {
            return DAL_sach.Search_LoaiSach(ten);
        }

        public static int laymaloai()
        {
            int ma = 0;
            List<BIZ_loaisach> ds = DAL_sach.Select_loaisach();
            ds = (from ls in ds
                  orderby ls.maloai descending
                  select ls).Take(1).ToList();
            foreach (BIZ_loaisach l in ds)
            {
                ma = l.maloai + 1;
                break;
            }
            return ma;
        }

        public static bool kiemtratrungtenloai(BIZ_loaisach loai)
        {
            
            List<BIZ_loaisach> ds = DAL_sach.Select_loaisach();
            foreach (BIZ_loaisach l in ds)
            {
                if (l.maloai != loai.maloai && l.tenloai.ToLower().Equals(loai.tenloai.ToLower()))
                    return true;
            }
            return false;
        }

        public static void Them_LoaiSach(BIZ_loaisach dto)
        {
            DAL_sach.Them_LoaiSach(dto);
        }

        public static void Sua_LoaiSach(BIZ_loaisach dto)
        {
            DAL_sach.Sua_LoaiSach(dto);
        }

        public static bool kiemtraxoa(BIZ_loaisach loai)
        {
            List<BIZ_sach> ds = DAL_sach.Select_sach(loai.maloai);
            foreach (BIZ_sach s in ds)
                if (loai.maloai == s.loai.maloai)
                    return false;
            return true;
        }

        public static void Xoa_loaisach(BIZ_loaisach loai)
        {
            DAL_sach.Xoa_loaisach(loai);
        }
    }
}
